package org.example.com.service.impl;

import org.example.com.mapper.HouseMapper;
import org.example.com.pojo.House;
import org.example.com.pojo.HouseTag;
import org.example.com.service.RentalDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租房数据服务实现类 - 为AI聊天提供数据支持
 */
@Service
public class RentalDataServiceImpl implements RentalDataService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public String getRentalDataSummary() {
        // 获取房源总数
        Long totalCount = houseMapper.countHouses(null, null, null, null, null, null, null);
        
        // 获取热门房源
        List<House> popularHouses = getPopularHouses(5);
        
        StringBuilder summary = new StringBuilder();
        summary.append("租房数据摘要：\n");
        summary.append("- 当前共有 ").append(totalCount).append(" 套可租房源\n");
        
        if (!popularHouses.isEmpty()) {
            summary.append("- 热门房源推荐：\n");
            for (House house : popularHouses) {
                summary.append("  * ").append(house.getHouseTitle())
                      .append("，").append(house.getRentArea())
                      .append("，").append(house.getRentPriceListing()).append(house.getRentPriceUnit())
                      .append("，").append(house.getAddress())
                      .append("\n");
            }
        }
        
        return summary.toString();
    }

    @Override
    public List<House> searchHousesByKeyword(String keyword) {
        // 使用现有的搜索方法，但限制结果数量为10条
        return houseMapper.searchHouses(keyword, null, null, null, 
                                      null, null, null, 
                                      "created_at", "desc", 0, 10);
    }

    @Override
    public List<House> getHousesByPriceRange(String minPrice, String maxPrice) {
        // 使用现有的搜索方法，按价格范围过滤
        return houseMapper.searchHouses(null, null, null, null, 
                                      minPrice, maxPrice, null, 
                                      "rent_price_listing", "asc", 0, 10);
    }

    @Override
    public List<House> getHousesByDistrict(String district) {
        // 这里简单处理，将district作为关键词搜索
        // 实际应用中可能需要更精确的匹配
        return houseMapper.searchHouses(district, null, null, null, 
                                      null, null, null, 
                                      "created_at", "desc", 0, 10);
    }

    @Override
    public List<House> getPopularHouses(int limit) {
        // 获取推荐房源，并限制数量
        List<House> houses = houseMapper.findNearbyHouses();
        if (houses.size() > limit) {
            houses = houses.subList(0, limit);
        }
        
        // 为每个房源加载标签
        for (House house : houses) {
            List<HouseTag> tags = houseMapper.findTagsByHouseId(house.getId());
            house.setTags(tags);
        }
        
        return houses;
    }

    @Override
    public House getHouseDetail(Integer houseId) {
        House house = houseMapper.findById(houseId);
        if (house != null) {
            // 加载房源标签
            List<HouseTag> tags = houseMapper.findTagsByHouseId(houseId);
            house.setTags(tags);
        }
        return house;
    }

    @Override
    public String formatHouseInfo(House house) {
        if (house == null) {
            return "未找到相关房源信息";
        }
        
        StringBuilder info = new StringBuilder();
        info.append("房源信息：").append(house.getHouseTitle()).append("\n");
        info.append("- 地址：").append(house.getAddress()).append("\n");
        info.append("- 面积：").append(house.getRentArea()).append("\n");
        info.append("- 租金：").append(house.getRentPriceListing()).append(house.getRentPriceUnit()).append("\n");
        info.append("- 付款方式：").append(house.getPaymentType()).append("\n");
        
        if (house.getTags() != null && !house.getTags().isEmpty()) {
            String tagStr = house.getTags().stream()
                .map(HouseTag::getName)
                .collect(Collectors.joining("、"));
            info.append("- 特点：").append(tagStr).append("\n");
        }
        
        if (house.getDescription() != null && !house.getDescription().isEmpty()) {
            info.append("- 描述：").append(house.getDescription()).append("\n");
        }
        
        return info.toString();
    }

    @Override
    public String formatHouseList(List<House> houses) {
        if (houses == null || houses.isEmpty()) {
            return "未找到符合条件的房源";
        }
        
        StringBuilder result = new StringBuilder();
        result.append("找到 ").append(houses.size()).append(" 套符合条件的房源：\n\n");
        
        for (int i = 0; i < houses.size(); i++) {
            House house = houses.get(i);
            result.append(i + 1).append(". ").append(house.getHouseTitle()).append("\n");
            result.append("   地址：").append(house.getAddress()).append("\n");
            result.append("   面积：").append(house.getRentArea()).append("\n");
            result.append("   租金：").append(house.getRentPriceListing()).append(house.getRentPriceUnit()).append("\n");
            
            if (house.getTags() != null && !house.getTags().isEmpty()) {
                String tagStr = house.getTags().stream()
                    .map(HouseTag::getName)
                    .collect(Collectors.joining("、"));
                result.append("   特点：").append(tagStr).append("\n");
            }
            
            result.append("\n");
        }
        
        return result.toString();
    }
}
